﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.ObjectModel;

namespace Prism.Collections.Generic
{
    [Serializable]
    public class Tree<NodeType, ValueType> : ITree<NodeType, ValueType>, IEnumerable<NodeType>, IEnumerable, ITreeEnumerating
        where NodeType : TreeNode<ValueType>
    {
        public Tree()
        {
            this.IsEnumerating = false;
        }

        public virtual void Add(NodeType parentNode, NodeType childNode, ValueType value)
        {
            if (this.IsEnumerating)
                throw new InvalidOperationOnEnumeratingException();
            if (childNode == null)
                throw new NullReferenceException("Paramenter 2: ChildNode can not be null.");
            if (parentNode == null)
            {
                if (this.TopNode != null)
                    throw new InvalidOperationException("The TopNode is already existed.");
                childNode.Parent = null;
                if (value != null)
                    childNode.Value = value;
                this.TopNode = childNode;
            }
            else
            {
                if (value != null)
                    childNode.Value = value;
                childNode.Parent = parentNode;
                parentNode.Children.Add(childNode);
            }
        }

        public virtual void RemoveLeaf(NodeType node)
        {
            if (this.IsEnumerating)
                throw new InvalidOperationOnEnumeratingException();
            if (node.Parent != null)
            {
                node.Parent.Children.Remove(node);
            }
            else
            {
                this.TopNode = null;
            }
            node = null;
        }

        public virtual void Clear()
        {
            if (this.IsEnumerating)
                throw new InvalidOperationOnEnumeratingException();
            this.TopNode = null;
        }

        public virtual NodeType TopNode
        {
            get;
            protected set;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            this.IsEnumerating = true;
            var r = this.IteratedGetAllNode(this.TopNode).GetEnumerator();
            this.IsEnumerating = false;
            return r;
        }

        public IEnumerator<NodeType> GetEnumerator()
        {
            this.IsEnumerating = true;
            var r = this.IteratedGetAllNode(this.TopNode).GetEnumerator();
            this.IsEnumerating = false;
            return r;
        }

        protected virtual IEnumerable<NodeType> IteratedGetAllNode(NodeType node)
        {
            if (node == null)
            {
                yield break;
            }
            else
            {
                foreach (NodeType i in node.Children)
                {
                    if (i.Children.Count == 0)
                    {
                        yield return i;
                    }
                    else
                    {
                        foreach (var j in this.IteratedGetAllNode(i))
                        {
                            yield return j;
                        }
                    }
                }
            }

            yield return node;
        }

        public bool IsEnumerating { get; protected set; }
    }
}
